package com.jd.sunny.java.multithreading.synch;

/**
 * @Description:
 * @author:sunny
 * @since:2012-12-12 19:08
 * @version:1.0.0
 */
public class BankRunnable implements Runnable{

    /**
     * volatile 貌似没起作用
     */
    private volatile Account from;
    private volatile Account to;

    public BankRunnable(Account from, Account to) {
        this.from = from;
        this.to = to;
    }

    public  void run() {
        try {
            while (true){
                // 随机相互转钱
                int i = (int)(10 * Math.random());
                if( i % 2 == 0){
                    Account.transfer(from,to,100);
                }else  if( i % 2 == 1){
                    Account.transfer(to,from,100);
                }
                Thread.sleep(i);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Account getTo() {
        return to;
    }

    public void setTo(Account to) {
        this.to = to;
    }

    public Account getFrom() {
        return from;
    }

    public void setFrom(Account from) {
        this.from = from;
    }
}
